package com.example.pclimb;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.pclimb.databinding.ActivityMainBinding;
import com.example.pclimb.model.User;
import com.example.pclimb.viewmodel.UserViewModel;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    private static final String PREF_NAME = "PclimbPrefs";
    private static final String KEY_ADMIN_CREATED = "admin_created";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // 使用ViewBinding
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setSupportActionBar(binding.toolbar);

        // 配置底部导航
        BottomNavigationView navView = binding.navView;

        NavHostFragment navHostFragment =
                (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
        if (navHostFragment == null) {
             navHostFragment = NavHostFragment.create(R.navigation.nav_graph);
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.nav_host_fragment, navHostFragment)
                    .setPrimaryNavigationFragment(navHostFragment)
                    .commitNow();
        }

        NavController navController = navHostFragment.getNavController();

        
        // 设置顶部Action Bar与导航联动，不显示返回按钮
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_requests, R.id.navigation_guides, R.id.navigation_me)
                .build();
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        
        // 设置底部导航与导航控制器联动
        NavigationUI.setupWithNavController(navView, navController);

        // 检查是否已经创建过管理员账号
        SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
        boolean adminCreated = prefs.getBoolean(KEY_ADMIN_CREATED, false);
        
        if (!adminCreated) {
            // 只有在未创建过管理员账号的情况下，才执行创建操作
            createAdminAccountIfNeeded();
        }
    }
    
    /**
     * 如果管理员账号不存在，则创建一个管理员账号
     */
    private void createAdminAccountIfNeeded() {
        UserViewModel userViewModel = new ViewModelProvider(this).get(UserViewModel.class);
        userViewModel.getUserByUsername("admin").observe(this, user -> {
            if (user == null) {
                // 管理员账号不存在，创建一个
                User admin = new User("0", "000000", User.TYPE_ADMIN);
                admin.setPhone("13800138000");
                admin.setEmail("admin@pclimb.com");
                
                userViewModel.insert(admin, id -> {
                    if (id > 0) {
                        Log.d("MainActivity", "创建管理员账号成功");
                        // 保存创建状态，避免重复创建
                        SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
                        prefs.edit().putBoolean(KEY_ADMIN_CREATED, true).apply();
                    }
                });
            } else {
                // 管理员账号已存在，记录创建状态
                SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
                prefs.edit().putBoolean(KEY_ADMIN_CREATED, true).apply();
            }
            // 只观察一次，避免重复触发
            userViewModel.getUserByUsername("admin").removeObservers(this);
        });
    }
}